package org.jeecg.modules.syncdata.handler;

import cn.hutool.core.date.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.syncdata.dto.SyncDataDto;
import org.jeecg.modules.syncdata.entity.SyncLog;
import org.jeecg.modules.syncdata.service.SyncLogService;
import org.jeecg.modules.syncdata.service.SyncService;
import org.jeecg.modules.thirdpartyapi.constant.BgApiEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static com.xxl.job.core.biz.model.ReturnT.SUCCESS;

/**
 * 同步处理程序
 *
 * @author zhanglei01
 * @date 2022/03/16
 */
@Slf4j
@Component

public class SyncCompensateHandler {

    @Autowired
    private SyncLogService syncLogService;

    @Autowired
    private SyncService syncService;

    @XxlJob(value = "syncCompensateHandler")
    public ReturnT<String> execute(String jobParam) throws Exception {
        List<SyncLog> syncFailDatas = syncLogService.getSyncFailData(1000);
        if (syncFailDatas.size() < 1) {
            log.info("没有数据需要同步");
            return SUCCESS;
        }

        ExecutorService executor = new ThreadPoolExecutor(5, 5, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        BgApiEnum[] values = BgApiEnum.values();
        for (SyncLog syncFailData : syncFailDatas) {
            SyncDataDto syncDataDto = new SyncDataDto();
            String begin = DateUtil.beginOfDay(DateUtil.parseDate(syncFailData.getDayTime())).toString();
            String end = DateUtil.endOfDay(DateUtil.parseDate(syncFailData.getDayTime())).toString();
            syncDataDto.setStartTime(begin);
            syncDataDto.setEndTime(end);
            //判断除当天外order是否全部完成
            Boolean syncResultExceptOrder = syncLogService.getSyncResultExceptOrder(syncFailData.getDayTime());
            if (!syncResultExceptOrder) {
                BgApiEnum bgApiEnum = null;
                for (BgApiEnum value : values) {
                    if (syncFailData.getSyncTable().equals(value.getValue())) {
                        bgApiEnum = value;
                    }
                }
                if (bgApiEnum == null) {
                    log.error("表接口不存在,{}", syncFailData.getSyncTable());
                    continue;
                }
                syncDataDto.setTableName(bgApiEnum.getValue());
                syncService.syncSingleTable(executor, bgApiEnum, syncDataDto, syncFailData.getDayTime());
            }
            //判断前一天和当天除order外是否全部同步完成，全部完成同步order表
            syncResultExceptOrder = syncLogService.getBeforeDaySyncResult(syncFailData.getDayTime());
            if (syncResultExceptOrder) {
                if (syncLogService.getSyncResult(syncFailData.getDayTime())) {
                    continue;
                }
                //同步order表
                ArrayList<BgApiEnum> bgApi = new ArrayList<>();
                bgApi.add(BgApiEnum.sjmkjg_order);
                bgApi.add(BgApiEnum.swzbjc_order);
                for (BgApiEnum bgApiEnum : bgApi) {
                    syncDataDto.setTableName(bgApiEnum.getValue());
                    syncService.syncSingleTable(executor, bgApiEnum, syncDataDto, syncFailData.getDayTime());
                }
            }
        }
        executor.shutdown();
        log.info("同步数据执行完成");
        return SUCCESS;
    }

}
